package com.hn658.base.message.managermq;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import com.hn658.base.message.managermq.contants.MQManagerContants;
import com.hn658.base.message.managermq.template.MqTemplate;
import com.hn658.base.message.managermq.template.MqTemplateResource;
import com.hn658.framework.logging.Logger;
import com.hn658.framework.logging.LoggerFactory;
import com.hn658.framework.logging.enums.LogCategory;

/**
 * MQ队列管理器
 *
 * @author ztjie
 */
@Component
public class JmsTemplateManager implements ApplicationContextAware {

	/**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(JmsTemplateManager.class);

    private  ConfigurableApplicationContext context;
    
    private XmlBeanDefinitionReader beanDefinitionReader;
    
    
    public JmsTemplateManager() {
		
	}
    public void init(){
    	beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)  
    			context.getBeanFactory());    
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(context));   
    }

    public JmsTemplate get(String appId, Integer index){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.TEMPLATE+"_"+key;
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
    				"获得Spring中应用ID为：{}，MQ序号为：{}的队列处理器", new Object[]{appId, index});
			return (JmsTemplate)context.getBean(beanName);
    	}else {
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中不存在应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{appId, index});
    		return null;
		}
    }
    
    /**
     * 
     * <p>通过应用ID及MQ队列序号，删除队列处理器</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 应用ID
     * @param index 应用配置的MQ队列序号
     * @return
     * @see
     */
//    public void remove(String appId, Integer index){
//    	String key = appId+"_"+index;
//    	String beanName=MQManagerContants.TEMPLATE+"_"+key;
//    	if(context.containsBean(beanName)){
//    		logger.debug(LogCategory.SYSTEM,
//    				"当前MQ_MANAGER中删除应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{appId, index});
//    		MqTemplate template=(MqTemplate)context.getBean(beanName);
//    	}else {
//    		logger.debug(LogCategory.SYSTEM,
//    				"当前MQ_MANAGER中不存在应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{appId, index});
//		}
//    }
    
    public void add(String appId, Integer index, MqTemplate template){
    	String key = appId+"_"+index;
    	logger.debug(LogCategory.SYSTEM,
				"当前Spring中增加应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{appId, index});
    	String beanName=MQManagerContants.TEMPLATE+"_"+key;
    	template.setName(beanName);
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
					"当前Spring中已存在应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{"", key});
			return;
    	}
    	logger.debug(LogCategory.SYSTEM,
				"当前Spring中增加应用ID为：{}，MQ队列序号为：{}的队列处理器", new Object[]{"", key});
    	beanDefinitionReader.loadBeanDefinitions(new MqTemplateResource(template));
    }
    
    @Override
   	public void setApplicationContext(ApplicationContext applicationContext)
   			throws BeansException {
   		context = (ConfigurableApplicationContext)applicationContext;
   		init();
   	}
}
